package de.dhbw.minf12.mas.voter;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import de.dhbw.minf12.mas.Permutation;
import de.dhbw.minf12.mas.logic.AbstractTargetFunctionLogic;

/**
 * Voter mit Konstruktions-Logik.
 * @author flo
 *
 */
public class ConstructingVoter extends AbstractVoter{

	public ConstructingVoter(AbstractTargetFunctionLogic pTargetFunction) {
		super(pTargetFunction);
	}
	
	/**
	 * Wählt den nächsten Job für die zu konstruierende Permutation.
	 * @param pCurrentContract
	 * @param pJobsLeftToPlan
	 * @return Job Nummer
	 */
	public int vote(List<Integer> pCurrentContractData, List<Integer> pJobsLeftToPlan){
		int ranking = Integer.MAX_VALUE;
		int bestLeftJob = Integer.MAX_VALUE;
		
		Iterator<Integer> leftJobIterator = pJobsLeftToPlan.iterator();
		while(leftJobIterator.hasNext()){
			Integer leftJob = leftJobIterator.next();
			List<Integer> newData = new LinkedList<Integer>(pCurrentContractData);
			newData.add(leftJob);
			Permutation permToTest = new Permutation(newData);
			
			int newRanking = this.targetFunction.calculateSolutionValue(permToTest);
			if(newRanking < ranking){
				ranking = newRanking;
				bestLeftJob = leftJob;
			}
		}
		
		return bestLeftJob;
	}
	
	public boolean vote(Permutation pCandidateContract, Permutation pCurrentContract){
		return this.targetFunction.calculateSolutionValue(pCandidateContract) <= this.targetFunction.calculateSolutionValue(pCurrentContract);
	}
}
